package com.jhh.pay.weishenma.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jhh.pay.weishenma.security.Base;
import com.jhh.pay.weishenma.service.WsmService;
import com.jhh.pay.weishenma.util.TestDataUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("/wsm")
public class WsmController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private WsmService wsmService;
	
	@Autowired
	private Base base;

	@RequestMapping(value="/callback", method={RequestMethod.POST})
	public String wsmCallBack(@RequestBody JSONObject req) {
		logger.info(req.toString());
		return "success";
	}
	
	@RequestMapping(value="/order/query", method= {RequestMethod.GET, RequestMethod.POST})
	public String wsmOrderQuery(@RequestBody JSONObject req) {
		logger.info("req : " + req.toString());
		String key= "bAt2fJsqKGMeuc8cEQ";
		StringBuilder builder = new StringBuilder();
		builder.append(key);
		JSONArray orderNos = req.getJSONArray("orderNo");
		if(orderNos == null) {
			return "orderNos 不能为空";
		}
		for(Object o : orderNos.toArray()) {
			builder.append(o.toString());
		}
		builder.append(req.getString("timestamp"));
		logger.info("before sign : " + builder);
		String sign = base.getMD5(builder.toString());
		logger.info("sign : " + sign);
		if(sign.equalsIgnoreCase(req.getString("sign"))) {
			JSONArray array = new JSONArray();
			for(Object o : orderNos.toArray()) {
				if("0117030209483323330".equals(o.toString())){
					JSONObject jo = new JSONObject();
					jo.put("shddh", "0117030209483323330");
					jo.put("userName", "王鸽");
					jo.put("idCard", "610523199508111025");
					jo.put("periodTotal", 1);
					jo.put("amount", "50000");
					jo.put("lastRepayAt", "2017-12-5");
					jo.put("loanSuccess", "1");
					JSONArray schedules = new JSONArray();
					JSONObject schedule = new JSONObject();
					schedule.put("dueAt", "2017-12-5");
					schedule.put("repayAtPartial", "2017-12-5");
					schedule.put("repayAt", "2017-12-5");
					schedule.put("status", "3");
					schedule.put("principal", "55000");
					schedule.put("repaidCapital", "50000");
					schedule.put("interest", "5000");
					schedule.put("repaidInterest", "5000");
					schedule.put("repaidPenalty", "5000");
					schedule.put("overdues", "0");
					schedules.add(schedule);
					jo.put("schedules", schedules);
					array.add(jo);
				}else if("0117030209483323331".equals(o.toString())) {
					JSONObject jo = new JSONObject();
					jo.put("shddh", "0117030209483323331");
					jo.put("userName", "王鸽");
					jo.put("idCard", "610523199508111025");
					jo.put("periodTotal", 1);
					jo.put("amount", "50000");
					jo.put("lastRepayAt", "2017-12-5");
					jo.put("loanSuccess", "1");
					JSONArray schedules = new JSONArray();
					JSONObject schedule = new JSONObject();
					schedule.put("dueAt", "2017-12-5");
					schedule.put("repayAtPartial", "2017-12-5");
					schedule.put("repayAt", "2017-12-5");
					schedule.put("status", "3");
					schedule.put("principal", "55000");
					schedule.put("repaidCapital", "50000");
					schedule.put("interest", "5000");
					schedule.put("repaidInterest", "5000");
					schedule.put("repaidPenalty", "5000");
					schedule.put("overdues", "0");
					schedules.add(schedule);
					jo.put("schedules", schedules);
					array.add(jo);
				}
			}
			logger.info(array.toString());
			return array.toString();
		}else {
			logger.info("request sign : " + req.getString("sign"));
			return "sign 验证失败";
		}
	}
	
	@RequestMapping(value="/run")
	public String wsmTestRun() {
		String result = wsmService.sendOrder(TestDataUtil.getTestData3());
		return result;
	}
	
	@RequestMapping(value="/runquery")
	public String wsmTestRunQuery() {
		String result = wsmService.checkOrderQuery(TestDataUtil.getTestData2().getString("shddh"));
		return result;
	}
}
